import router from './router'
import NProgress from "nprogress"; // 导入 nprogress模块
import "nprogress/nprogress.css"; // 导入样式，否则看不到效果

import { createPinia } from 'pinia';
// let pinia = createPinia()
import pinia from './stores';

import { useUserStore } from './stores/user';
let userStore = useUserStore(pinia)

//存储用户未登录也可以访问的网址
let whiteList = ['/home','/hospitalDetail/register','/hospitalDetail/detail','/hospitalDetail/notice','/hospitalDetail/close','/hospitalDetail/search']

NProgress.configure({ showSpinner: false }); // 显示右上角螺旋加载提示

router.beforeEach((to, from, next) => {
NProgress.start(); //开启进度条
document.title = '尚医通-' + to.meta.title
console.log(userStore, '12');
let token = userStore.userInfo.token
console.log(token);

if (token) {
  next()
} else {
  console.log(to);
  if (whiteList.includes(to.path)) {
    next()
  } else {
    userStore.dialogVisible = true
    // userStore.changeDialog('open')
    
    next({
      path: '/home',
      query: {
        redirect: to.fullPath
      }
    })
  }
}
})


router.afterEach(() => {
  NProgress.done(); //完成进度条
})